package com.chenglai.classify.service;

import com.chenglai.classify.dao.ClassifyPicMapper;
import com.chenglai.classify.dao.GoodsClassifyMapper;
import com.chenglai.classify.pojo.ClassifyPic;
import com.chenglai.classify.pojo.GoodsClassify;
import com.chenglai.commonutils.common.OSSUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartRequest;
import tk.mybatis.mapper.entity.Example;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * @author yawang
 * @create 2023/11/14 17:37:04
 **/
@Service
public class GoodsClassifyServiceImpl implements GoodsClassifyService{

    @Autowired
    private GoodsClassifyMapper goodsClassifyMapper;
    @Autowired
    private ClassifyPicMapper picMapper;
    @Override
    public void addClassify(GoodsClassify classify) {

        goodsClassifyMapper.insertSelective(classify);


    }

    @Override
    public List<GoodsClassify> listGoodsClassify(Integer shopId,Integer parentId) {
        Example example = new Example(GoodsClassify.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("shopId",shopId).andEqualTo("parentId",parentId);
        example.orderBy("classifyId").desc();
        return goodsClassifyMapper.selectByExample(example);

    }

    @Override
    public GoodsClassify classifyDetail(Integer classifyId) {

        return goodsClassifyMapper.selectByPrimaryKey(classifyId);
    }

    @Override
    public Map deleteClassify(Integer classifyId,Integer classifyType) {
        Map map = new HashMap();
        //获取商品信息
        int size = 0;
        if(size==0){
            Example example = new Example(GoodsClassify.class);
            Example.Criteria criteria = example.createCriteria();
            criteria.andEqualTo("classifyId",classifyId);
            if(classifyType==3){

                goodsClassifyMapper.deleteByExample(example);
            }
            if(classifyType==2){
                criteria.orEqualTo("parentId",classifyId);
                goodsClassifyMapper.deleteByExample(example);
            }
            if(classifyType==1){
               //获取二级和三级
                criteria = example.createCriteria();
                criteria.andEqualTo("parentId",classifyId);
                List<GoodsClassify> secondList = goodsClassifyMapper.selectByExample(example);
                List<Integer> ids= new ArrayList<>();
                for (GoodsClassify classify : secondList) {
                    ids.add(classify.getClassifyId());
                }
                //删除二级
                goodsClassifyMapper.deleteByExample(example);
                //删除三级
                criteria = example.createCriteria();
                criteria.andIn("parentId",ids);
                goodsClassifyMapper.deleteByExample(example);
                //删除一级
                goodsClassifyMapper.deleteByPrimaryKey(classifyId);

            }
            map.put("flag","success");
            map.put("msg","删除成功");

        }else{
            map.put("flag","error");
            map.put("msg","所选分类下还有商品，无法删除");
        }
        return map;
    }

    @Override
    public void addClassifyPics(HttpServletRequest request) {
        List<MultipartFile> picList = ((MultipartRequest) request).getFiles("file");
        List<ClassifyPic> classifyPics = new ArrayList<>();
        ClassifyPic pic ;
        String filename;
        if(picList.size()>0){
            for (MultipartFile file : picList) {
                pic = new ClassifyPic();
                filename = OSSUtil.uploadingImg(file,"shangcheng/goodsClassify");
                pic.setPicName(filename);
                pic.setPicStatus(1);
                classifyPics.add(pic);
            }
            picMapper.insertList(classifyPics);
        }
    }

    @Override
    public void delClassifyPic(String ids) {
        Example example = new Example(ClassifyPic.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andIn("classifyPic", Arrays.asList(ids));
        List<ClassifyPic> pics = picMapper.selectByExample(example);
        for (ClassifyPic pic : pics) {
            OSSUtil.deleteFile(pic.getPicName());
        }
        picMapper.deleteByExample(example);


    }
}
